package com.luyu.customvalid;


import cn.hutool.core.lang.Pair;
import org.hibernate.validator.spi.group.DefaultGroupSequenceProvider;

import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.function.Function;

/**
 * @Author lyh
 */
public abstract class AbstractGroupSequenceProvider<T> implements DefaultGroupSequenceProvider<T> {

    @Override
    public List<Class<?>> getValidationGroups(T object) {
        List<Class<?>> groupList = new ArrayList<>();
        // 因为重新覆盖默认的default分组所以需要将当前类的class加入，不然无法进行校验
        groupList.add(this.getType());

        if (object == null) {
            return groupList;
        }

        // 添加分组
        this.handle(object, groupList);

        return groupList;
    }

    /**
     * 获取当前类的class
     * @return
     */
    public Class<T> getType() {
        Type type = getClass().getGenericSuperclass();
        ParameterizedType paramType = (ParameterizedType) type;
        Type[] argTypes = paramType.getActualTypeArguments();
        return (Class<T>) argTypes[0];
    }
    protected void handle(T obj, List<Class<?>> groupList) {

    }
}

